package com.fick.tags;

/**
 * 显示指定channelid的栏目图片
 */
import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.ChannelDAO;
import com.fick.hibernate.model.Channel;

public class ChannelImgTag extends TagSupport {
	private static Log log = LogFactory.getLog(NewsLocationTag.class);

	// 栏目id
	private int channelid;
	// 是否带连接
	private boolean isLink = false;
	// <a>标签的属性
	private String linkAttr = "";
	// 图片属性
	private String imgAttr = "";

	@Override
	public int doStartTag() throws JspException {

		try {
			StringBuffer sb = new StringBuffer();
			ChannelDAO dao = new ChannelDAO();
			Channel channel = dao.findById(channelid);
			if (channel != null) {
				if (isLink) {
					sb.append("<a href='");
					sb.append(channel.getUrl());
					sb.append("?channelid=");
					sb.append(channelid);
					sb.append("' ");
					sb.append(linkAttr);
					sb.append(" >");
					sb.append("<img src='");
					sb.append(channel.getImg());
					sb.append("' ");
					sb.append(imgAttr);
					sb.append(" />");
					sb.append("</a>");
				} else {
					sb.append("<img src='");
					sb.append(channel.getImg());
					sb.append("' ");
					sb.append(imgAttr);
					sb.append(" />");
				}
			}
			JspWriter out = pageContext.getOut();
			out.println(sb.toString());
			out.flush();
		} catch (IOException e) {
			log.error("标签显示错误" + e);
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public int getChannelid() {
		return channelid;
	}

	public String getImgAttr() {
		return imgAttr;
	}

	public boolean getIsLink() {
		return isLink;
	}

	public String getLinkAttr() {
		return linkAttr;
	}

	public void setChannelid(int channelid) {
		this.channelid = channelid;
	}

	public void setImgAttr(String imgAttr) {
		this.imgAttr = imgAttr;
	}

	public void setIsLink(boolean isLink) {
		this.isLink = isLink;
	}

	public void setLinkAttr(String linkAttr) {
		this.linkAttr = linkAttr;
	}

}
